/*
 * @Author: Black / 李来然
 * @Date: 2021-12-20 10:20:56
 * @LastEditors: Black / 李来然
 * @LastEditTime: 2021-12-20 16:43:52
 * @Description: 指纹验证流程
 */
import { useModule } from "../createMod";

import { FPCfnRet } from "@/ecjrts/mode/fpc";
import { DirStruct } from "../utils";

/**
 *采集指纹特征组件
 * @returns ReadSuccessful 读取特征值成功
 * @returns FingerDetected 手指按下
 * @returns FingerTaken 手指移开
 * @returns DeviceError 设备错误
 * @returns FatalError 其他错误
 */
type FpcFn = (param: FPCfnRet) => void;

const down = "请按下手指";
const upFinger = "请移开手指";

export const ReadTemplate = async function (
  timeout: number = 90000,
  fileName: string,
  callback?: FpcFn
) {
  Trace(EC_INFO, `->ReadTemplate`);
  const { fpc, glt, common } = useModule();

  if (!fpc) throw new Error(`没有fpc模块`);
  
  if (!common) throw new Error(`没有fpc模块`);
  let isPathFileExist = await common.IsPathFileExists('c:\\finger\\template');
	console.log(isPathFileExist);
	if(isPathFileExist !== 0) {
		await common.CreateDir('c:\\finger\\template')
	}

  await glt?.SetFingerPrint("MEDIUM");
  let retReadEigen = await fpc.ReadTemplate(
    timeout,
    `c:\\finger\\template\\${fileName}`,
    async (ret) => {
      switch (ret.ret) {
        case "FingerDetected":
          if (callback) callback(ret.ret);
          // return ret.ret
          break;
        case "FingerTaken":
          if (callback) callback(ret.ret);
          // return ret.ret
          break;
      }
      Trace(EC_INFO, `--ReadTemplate ret=${ret.ret}`);
    }
  );
  await glt?.SetFingerPrint("OFF");
  // console.log(retReadEigen);
  if (retReadEigen) {
    if (retReadEigen.ret == "ReadSuccessful") {
      Trace(EC_INFO, `<-ReadTemplate ret=${retReadEigen.ret}`);
      return retReadEigen.ret;
    } else {
      Trace(EC_INFO, `<-ReadTemplate ret=DeviceError`);
      return "DeviceError";
    }
  } else {
    Trace(EC_INFO, `<-ReadTemplate ret=FatalError`);
    return "FatalError";
  }
};

export const ReadEigenvalue = async function (
  timeout: number = 90000,
  path: string = "C:\\finger.bmp",
  callback?: FpcFn
) {
  Trace(EC_INFO, `->ReadEigenvalue`);

  const { fpc, glt, common } = useModule();

  if (!fpc) throw new Error(`没有fpc模块`);

  await glt?.SetFingerPrint("MEDIUM");

  let retReadEigen = await fpc.ReadEigenvalue(
    timeout,
    path,
    async (ret) => {
      console.log(ret);

      switch (ret.ret) {
        case "FingerDetected":
          if (callback) callback(ret.ret);
          break;
        case "FingerTaken":
          if (callback) callback(ret.ret);
          break;
      }
      Trace(EC_INFO, `--ReadEigenvalue ret=${ret.ret}`);
    }
  );
  await glt?.SetFingerPrint("OFF");
  // console.log(retReadEigen);
  if (retReadEigen) {
    if (retReadEigen.ret == "ReadSuccessful") {
      Trace(EC_INFO, `<-ReadEigenvalue ret=${retReadEigen.ret}`);
      // const img64 = common?.ReadImageBase64(strPath).then((value) => {
      //   return value;
      // });
      // await Promise.all([img64]);
      return retReadEigen.ret;
    } else {
      Trace(EC_INFO, `<-ReadEigenvalue ret=DeviceError`);
      return "DeviceError";
    }
  } else {
    Trace(EC_INFO, `<-ReadEigenvalue ret=FatalError`);
    return "FatalError";
  }
};

/**
 * 取消等待用户按指纹
 * @returns WaitCanceled 命令取消
 * @returns DeviceError 设备错误
 * @returns FatalError 其他错误
 */
export const CancelWaitAccept = async function () {
  Trace(EC_INFO, `->CancelWaitAccept`);
  const { fpc, glt, common } = useModule();

  if (!fpc) throw new Error(`没有fpc模块`);
  await glt?.SetFingerPrint("OFF");
  const ret = await fpc.CancelWaitAccept();
  if (ret) {
    Trace(EC_INFO, `<-CancelWaitAccept ret=${ret.ret}`);
    return ret.ret;
  } else {
    Trace(EC_INFO, `<-CancelWaitAccept ret=FatalError`);
    return "FatalError";
  }
};

export const VerifyFinger = async () => {
  Trace(EC_INFO, `->VerifyFinger`);
  const { fpc, glt, common } = useModule();

  if (!fpc) throw new Error(`没有fpc模块`);
  let dirPath = `c:\\finger\\template`;
  let templateArr = await DirStruct(dirPath);
  if (templateArr.length > 0) {
    let arr = [];
    for (const fileName of templateArr) {
      arr.push(`${dirPath}\\${fileName}`);
    }
    let ret = await fpc.Verify(arr);
    Trace(EC_INFO, `-- VerifyFinger evt= ${ret.ret}`);
    switch (ret.ret) {
      case "DataNotSupport":
      case "DataMissing":
      case "ReadSuccessful":
        Trace(EC_INFO, `<- VerifyFinger ret=${ret.ret}`);
        return ret.ret;
      case "DeviceError":
      case "FatalError":
        return ret.ret;
    }
  } else {
    return "DataMissing";
  }
};

export const GetEigenvalue = async function () {
  Trace(EC_INFO, `->ResetFinger`);
  const { fpc, glt, common } = useModule();

  if (!fpc) throw new Error(`没有fpc模块`);
  await glt?.SetFingerPrint("OFF");
  let ret = await fpc.GetEigenvalueSync();
  return ret;
};

export const ResetFinger = async function () {
  Trace(EC_INFO, `->ResetFinger`);
  const { fpc, glt, common } = useModule();

  if (!fpc) throw new Error(`没有fpc模块`);
  await glt?.SetFingerPrint("OFF");
  let ret = await fpc.Reset();
  switch (ret.ret) {
    case "ResetComplete":
      return ret.ret;
    case "Timeout":
      return ret.ret;
    case "DeviceError":
      return ret.ret;
    case "FatalError":
      return ret.ret;
  }
};
